/**  
* Copyright (c) 2008, Noel Lynch All rights reserved. Redistribution and use  
* in source and binary forms, with or without modification, are permitted  
* provided that the following conditions are met: Redistributions of source  
* code must retain the above copyright notice, this list of conditions and the  
* following disclaimer. Redistributions in binary form must reproduce the above  
* copyright notice, this list of conditions and the following disclaimer in the  
* documentation and/or other materials provided with the distribution. Neither  
* the name of Noel Lynch nor the names of contributors may be used to  
* endorse or promote products derived from this software without specific prior  
* written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT  
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A  
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR  
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,  
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,  
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;  
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,  
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR  
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF  
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  
*/
// Jul 30, 2008

package com.sceneworker.monitorpopup.panels;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JPanel;

import com.jme.animation.SpatialTransformer;
import com.jme.math.Quaternion;
import com.jme.math.Vector3f;
import com.jme.renderer.Renderer;
import com.jme.scene.Controller;
import com.jme.scene.Spatial;
import com.jme.system.DisplaySystem;
import com.myjavautils.swing.SwingComponentFactory;
import com.sceneworker.editors.beanshelleditor.BeanShellController;

public class NewControllerPanel extends JPanel {

	private		JDialog		mcl_owner;
	private		Spatial		mcl_parent;
	
	public
	NewControllerPanel(JDialog cl_owner, Spatial cl_parent) {
		super();
		
		mcl_owner = cl_owner;
		mcl_parent = cl_parent;
		
		build();
	}
	
	private	void
	build() {
		final	JComboBox		lcl_combo = SwingComponentFactory.makeAComboBox();
		final	Renderer		lcl_render = DisplaySystem.getDisplaySystem().getRenderer();
		add(lcl_combo);
		lcl_combo.addItem(new ControllerEntry(SpatialTransformer.class, new CreateControllerFunc() {

			public Controller createController() {
				SpatialTransformer		lcl_tx = new SpatialTransformer(1);
				lcl_tx.setObject(mcl_parent, 0, -1);
				lcl_tx.setPosition(0, 0, new Vector3f(mcl_parent.getLocalTranslation()));
				lcl_tx.setScale(0, 0, new Vector3f(mcl_parent.getLocalScale()));
				lcl_tx.setRotation(0, 0, new Quaternion(mcl_parent.getLocalRotation()));
				return lcl_tx;
			}
		}));
		
		lcl_combo.addItem(new ControllerEntry(BeanShellController.class, new CreateControllerFunc() {

			public Controller createController() {
				BeanShellController		lcl_tx = new BeanShellController();
				lcl_tx.setSpatial(mcl_parent);
				
				return lcl_tx;
			}
		}));
		
		JButton		lcl_addState = SwingComponentFactory.makeAButton("Add Controller");
		lcl_addState.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				ControllerEntry		lcl_e = (ControllerEntry)lcl_combo.getSelectedItem();
				
				if(lcl_e == null) { return; }
				
				Controller		lcl_controller = null;
				
				lcl_controller = lcl_e.mcl_createControllerFunc.createController();
				
				if(lcl_controller == null) { return; }
				
				mcl_parent.addController(lcl_controller);
				mcl_parent.updateGeometricState(0, true);
			}
			
		});
		
		add(lcl_addState);
		
		JButton		lcl_done = SwingComponentFactory.makeAButton("Done");
		lcl_done.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				mcl_owner.setVisible(false);
			}
			
		});
		add(lcl_done);
	}
	
	private	interface	CreateControllerFunc {
		Controller		createController();
	}
	
	private	class	ControllerEntry {
		public	Class						mcl_controllerClass;
		public	CreateControllerFunc		mcl_createControllerFunc;
		
		public
		ControllerEntry(Class cl_class, CreateControllerFunc cl_func) {
			mcl_controllerClass = cl_class;
			setCreateControllerFunc(cl_func);
		}
		
		@Override
		public String toString() {
			return mcl_controllerClass.getSimpleName();
		}
		
		public	void
		setCreateControllerFunc(CreateControllerFunc cl_func) {
			mcl_createControllerFunc = cl_func;
		}
		
	}
}
